/**
 * Copyright (c) 2011 Nokia Corporation.
 */

import QtQuick 1.1
import com.nokia.meego 1.0
import "HarmattanConstants.js" as Constants

Page {
    id: sensorListPage
    orientationLock: root.orientation

    // Sensor reading page declaration. This page will be pushed in the page
    // stack when a sensor has been selected.
    SensorReadingPage {
        id: sensorReadingPage
        orientationLock: root.orientation
    }

    ListView {
        id: sensorListView
        anchors.fill: parent
        model: sensorModel

        delegate: Item {
            id: listItem
            width: sensorListPage.width
            height: Constants.DEFAULT_LIST_ITEM_HEIGHT

            // Hilight rectangle
            Rectangle {
                anchors.fill: parent
                color: "#2222ee"
                visible: listItemMouseArea.pressed
            }

            Row {
                id: listItemRow
                x: Constants.DEFAULT_MARGIN
                width: parent.width - Constants.DEFAULT_MARGIN * 2;
                height: parent.height

                Text {
                    width: listItemRow.width - arrowImage.width
                    height: listItemRow.height
                    color: "white"
                    verticalAlignment: Text.AlignVCenter
                    font.pixelSize: Constants.DEFAULT_FONT_PIXEL_SIZE
                    font.bold: true
                    text: type
                }

                Image {
                    id: arrowImage
                    y: (listItemRow.height - sourceSize.height) / 2
                    source: "image://theme/icon-m-common-drilldown-arrow"
                            + (theme.inverted ? "-inverse" : "");
                }
            }

            MouseArea {
                id: listItemMouseArea
                anchors.fill: parent

                onClicked: {
                    var success = sensorManager.setSensorType(type);

                    if (success) {
                        pageStack.push(sensorReadingPage);
                    }
                    else {
                        console.debug("Failed to set the sensor!");
                        errorDialog.text =
                            "Failed to start the sensor! This could " +
                            "indicate that the sensor is not supported " +
                            "by the device.";
                        errorDialog.open();
                        hideTimer.restart();
                    }
                }
            }
        }
    }

    ScrollDecorator {
        flickableItem: sensorListView
    }

    Dialog {
        id: errorDialog
        property alias text: contentItem.text

        content: Item {
            width: parent.width

            Text {
                id: contentItem
                width: parent.width
                color: "white"
                font.pixelSize: Constants.DEFAULT_FONT_PIXEL_SIZE
                wrapMode: Text.WordWrap
            }
        }

        Timer {
            id: hideTimer
            interval: 3000
            repeat: false
            onTriggered: errorDialog.close();
        }
    }

    Dialog {
        id: infoDialog
        visualParent: sensorListPage

        content: InfoContent {
            id: infoContent
            width: parent.width
            height: parent.height
            anchors.centerIn: parent
            anchors.margins: Constants.DEFAULT_MARGIN
            z: 1
            fontPixelSize: 24
        }
    }

    // Orientation setting selection menu
    Menu {
        id: orientationMenu
        visualParent: pageStack

        MenuLayout {
            MenuItem {
                text: "Auto orientation";
                onClicked: root.orientation = PageOrientation.Automatic;
            }
            MenuItem {
                text: "Lock to portrait";
                onClicked: root.orientation = PageOrientation.LockPortrait;
            }
            MenuItem {
                text: "Lock to landscape";
                onClicked: root.orientation = PageOrientation.LockLandscape;
            }
            MenuItem {
                text: "About";
                onClicked: infoDialog.open();
            }

        }
    }

    tools: ToolBarLayout {
        Item {} // In order to align the menu icon to the right
        ToolIcon {
            iconId: "toolbar-view-menu";
            onClicked: (orientationMenu.status == DialogStatus.Closed) ?
                           orientationMenu.open() : orientationMenu.close();
        }
    }
}

// End of file.
